package com.tanhua.dubbo.api;

import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.tanhua.dubbo.api.db.SettingsApi;
import com.tanhua.dubbo.api.mapper.SettingsMapper;
import com.tanhua.model.db.Settings;
import org.apache.dubbo.config.annotation.DubboService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;

/**
 * 通知设置服务接口
 */
@DubboService
public class SettingsApiImpl implements SettingsApi {

    @Autowired
    private SettingsMapper settingsMapper;

    /**
     * 2根据当前用户id调用服务查询通知设置
     * @param userId
     * @return
     */
    @Override
    public Settings findByUserId(Long userId) {
        QueryWrapper<Settings> queryWrapper = new QueryWrapper();
        queryWrapper.eq("user_id",userId);
        return settingsMapper.selectOne(queryWrapper);
    }

    /**
     * 通知设置 - 保存
     */
    @Override
    public void saveSettings(Settings settings) {
        //1根据当前用户id查询通知设置表记录是否存在
        Settings st = findByUserId(settings.getUserId());
        //2不存在 则保存
        if(st == null){
            settingsMapper.insert(settings);
        }else  {
            //3存在 则更新
            settings.setId(st.getId());//用户id
            settingsMapper.updateById(settings);
        }
    }
}
